<?php
App::uses('AppController', 'Controller');

class CurriculosController extends AppController {

	public $uses = array();
        function beforeFilter() {
            parent::beforeFilter();
            $AppInformation['Controller']['nameSingular'] = "Curriculoe";
            $AppInformation['Controller']['namePlural'] = "Curriculoes";
            $this->set("AppInformation",$AppInformation);
        }

        function index()
        {
            $this->layout = "admin";
            $Curriculos = $this->Curriculo->find("all");

            $this->set('Curriculos',$Curriculos);
        }
        function add()
        {
            $this->layout = "default";

						//die(debug($this->inverteData("20/07/1992")));



            if ($this->request->is('post'))
            {
                //die(debug($this->request->data));


								//die(debug($this->Curriculo->find('all')));
                //$this->Curriculo->createWithAttachment($this->request->data['Curriculo']);

								$this->request->data['Curriculo']['user_id']=$this->Auth->user('id');

								$this->Curriculo->createWithAttachments($this->request->data);

								$id = $this->Curriculo->getLastInsertId();

								foreach($this->request->data['Formacao'] as $Formacao)
								{
									//die(debug($Formacao));
									$Formacao['curriculo_id']=$id;
									$this->Curriculo->Formacao->create();
										$this->Curriculo->Formacao->save($Formacao);
								}
								foreach($this->request->data['Curso'] as $Curso)
								{
									//die(debug($Formacao));
									$Curso['curriculo_id']=$id;
									$this->Curriculo->Curso->create();
										$this->Curriculo->Curso->save($Curso);
								}
								foreach($this->request->data['Portfolio'] as $Portfolio)
								{
									//die(debug($Formacao));
									$Portfolio['curriculo_id']=$id;
									$this->Curriculo->Portfolio->create();
										$this->Curriculo->Portfolio->save($Portfolio);
								}

								foreach($this->request->data['Experiencia'] as $Experiencia)
								{
									//die(debug($Formacao));
									$Experiencia['curriculo_id']=$id;
									$this->Curriculo->Experiencia->create();
									$this->Curriculo->Experiencia->save($Experiencia);
								}

								foreach($this->request->data['Funcao'] as $Funcao)
								{
									//die(debug($Formacao));
									$Funcao['curriculo_id']=$id;
									$this->Curriculo->Funcao->create();
									$this->Curriculo->Funcao->save($Funcao);
								}

								$this->request->data['User']['id'] = $this->Auth->user('id');
								$this->Curriculo->User->save($this->request->data['User']);

								$this->request->data['ExpArea']['curriculo_id']=$id;
								$this->Curriculo->ExpArea->save($this->request->data['ExpArea']);

								$this->request->data['ExpGenero']['curriculo_id']=$id;
								$this->Curriculo->ExpGenero->save($this->request->data['ExpGenero']);


								$this->request->data['ObsAtor']['curriculo_id']=$id;
								$this->Curriculo->ObsAtor->save($this->request->data['ObsAtor']);


								$this->request->data['ObsLocutor']['curriculo_id']=$id;
								$this->Curriculo->ObsLocutor->save($this->request->data['ObsLocutor']);

								//die(debug($id));
								$this->redirect(array('action'=>'success'));
                $this->Session->setFlash("Salvo com sucesso!");

                // Trocar 2 por getInsertLastInsert
                //$this->redirect(array('action'=>'edit',2));
            }
        }

				function success()
				{
					$this->layout = "default";
				}

        function edit($id=null)
        {
            $this->layout = "admin";

            if ($this->request->is('post'))
            {
//                die(debug($this->request->data));
                $this->Curriculo->save($this->request->data);
                $this->Session->setFlash(__('Salvo com sucesso!'), 'Flashs/FlashMessengeGood');
            }
            else
            {
                $this->data = $this->Curriculo->find("first", array("recursive"=>2,"conditions"=>array("id"=>$id)));
            }
        }
        function delete($id)
        {
            $this->Propose->delete($id);
            $this->Session->setFlash('Removido com sucesso.', 'default', array(), 'good');
            $this->redirect('/admin/pages');
        }
}
